import { ref } from 'vue'
import { defineStore } from 'pinia'
import {getUserInfoServer,logoutServer} from '@/api/user'

export const useUserStore = defineStore(
  'book-user',
  () => {
    //用户登录凭证管理
    const token = ref(null)
    const setToken = (value) => {
      token.value = value
    }
    const removeToken = () => {
      token.value = null
    }
    // 用户信息管理
    const user = ref(null)
    const getUser = async () => {
      const res = await getUserInfoServer()
      user.value = res?.data
    }
    const setUser = (userInfo) => {
      user.value = userInfo
    }
    const logout = async () => {
      removeToken()
      user.value = null
      await logoutServer()
    }

    return {
      token,
      setToken,
      removeToken,
      user,
      getUser,
      setUser,
      logout
    }
  },
  {
    persist: true
  }
)
